<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'process:delay:pmDelay:add'">
          <Icon icon="fluent:add-12-filled" /> {{ t('发起申请') }}
        </a-button>
      </template> -->
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.createDate }}
        </a>
      </template>

      <template #projectno="{ record }">
        <a @click="handleDetailsForm(record)">
          {{ record.projectno }}
        </a>
      </template>

      <template #uploadFile="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'PmDelay_image'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProcessDelayPmDelayList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { findWarningList } from '/@/api/proj/info/pmProjectinformation';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import { BasicUpload } from '/@/components/Upload';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('process.delay.pmDelay');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('即将超期项目'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('项目编码'),
        field: 'projectno',
        component: 'Input',
      },
      {
        label: t('项目名称'),
        field: 'projectname',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    // {
    //     title: t('申请时间'),
    //     dataIndex: 'createDate',
    //     key: 'a.createtime',
    //     sorter: true,
    //     width: 130,
    //     align: 'center',
    //   },
    {
      title: t('项目编码'),
      dataIndex: 'projectno',
      key: 'a.pcode',
      sorter: true,
      width: 160,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('实际工期'),
      dataIndex: 'actualduration',
      // key: 'a.actualduration',
      // sorter: true,
      width: 100,
      align: 'center',
    },

    // {
    //   title: t('开始日期'),
    //   dataIndex: 'starttime',
    //   key: 'a.starttime',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    // {
    //   title: t('结束日期'),
    //   dataIndex: 'endtime',
    //   key: 'a.endtime',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },

    // {
    //   title: t('延期原因'),
    //   dataIndex: 'remarks',
    //   key: 'a.delayreason',
    //   sorter: true,
    //   width: 160,
    //   align: 'left',
    // },
    // {
    //   title: t('延期天数'),
    //   dataIndex: 'delayday',
    //   key: 'a.delayday',
    //   sorter: true,
    //   width: 100,
    //   align: 'center',
    // },
    {
      title: t('剩余天数'),
      dataIndex: 'syDay',
      // key: 'a.syDay',
      // sorter: true,
      width: 100,
      align: 'center',
    },
    // {
    //   title: t('佐证图片'),
    //   dataIndex: 'uploadFile',
    //   width: 100,
    //   align: 'center',
    //   // fixed: 'right',
    //   slot: 'uploadFile',
    // },
    {
      title: t('项目状态'),
      dataIndex: 'projStatus',
      // key: 'a.status',
      // sorter: true,
      width: 100,
      align: 'center',
      dictType: 'zf_proj_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑延期豁免'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'process:delay:pmDelay:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除延期豁免'),
        popConfirm: {
          title: t('是否确认删除延期豁免'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'process:delay:pmDelay:delete',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: findWarningList,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  async function handleDelete(record: Recordable) {
    const res = await pmDelayDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'Flow_Delay', bizKey: record.id });
  }
  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
  }
</script>
